#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define maxsize 8

typedef int ElemType;

typedef struct{
    ElemType data[maxsize];
    int front; // 对头指针
    int rear;  // 队尾指针  
}SqQueue; // 循环队列

void initQueue(SqQueue* Q); // 初始化队列
bool isEmpty(SqQueue* Q); // 判断队列是否为空
bool enQueue(SqQueue* Q, ElemType value); // 进队操作
bool deQueue(SqQueue* Q, ElemType* value); // 出栈操作
bool getHead(SqQueue* Q, ElemType* value); // 获取队头元素,但是不出队

int main()
{
    SqQueue* Q = (SqQueue*) malloc(sizeof(SqQueue));
    initQueue(Q);
    for(int i = 1; i < 10; i++)
    {
        enQueue(Q, i);
    }
    ElemType data;
    getHead(Q, &data);
    printf("Head: %d\n", data);
    while(!isEmpty(Q))
    {
        if(deQueue(Q, &data))
        {
            printf("%d ", data);
        }
    }
    return 0;
}

void initQueue(SqQueue* Q)
{
    Q->front = 0;
    Q->rear = 0;
}

bool isEmpty(SqQueue* Q)
{
    return Q->front == Q->rear;
}

bool enQueue(SqQueue* Q, ElemType value)
{
    if((Q->rear+1) % maxsize == Q->front)
    {
        printf("QUeue is filled\n");
        return false;
    }
    Q->data[Q->rear] = value;
    Q->rear = (Q->rear + 1) % maxsize;
    return true;
}


bool deQueue(SqQueue* Q, ElemType* value)
{
    if(isEmpty(Q))
    {
        printf("Queue is Empty\n");
        return false;
    }
    *value = Q->data[Q->front];
    Q->front = (Q->front + 1) % maxsize;
    return true;
}


bool getHead(SqQueue* Q, ElemType* value)
{
    if(isEmpty(Q))
    {
        printf("Queue is Empty\n");
        return false;
    }
    *value = Q->data[Q->front];
    return true;
}